package com.shop.cereshop.agent.service.wallet.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.agent.dao.wallet.AgentWalletDAO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.page.wallet.AgentWalletVO;
import com.shop.cereshop.agent.param.user.LoginDTO;
import com.shop.cereshop.agent.service.wallet.AgentWalletService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.wallet.CereAgentWallet;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EncryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 代理商钱包服务实现类
 *
 * @author
 * @since 2025-11-14
 */
@Slf4j
@Service
public class AgentWalletServiceImpl extends ServiceImpl<AgentWalletDAO, CereAgentWallet> implements AgentWalletService {

  @Autowired
  private AgentWalletDAO agentWalletDAO;

  @Override
  public int updatePayPassword(LoginDTO param) throws CoBusinessException {
    if (param != null) {
      param.setPassword(EncryptUtil.encrypt(param.getPassword()));
    } else {
      throw new CoBusinessException(CoReturnFormat.PARAM_INVALID);
    }
    return agentWalletDAO.updatePayPassword(param);
  }

  @Override
  public AgentWalletVO getWallet(Long agentUserId, Integer roleType) {
    // 根据不同角色类型获取钱包信息
    if (roleType.equals(IntegerEnum.ROLE_TYPE_AGENT.getCode())) {
      // 查询钱包基础信息
      AgentWalletVO wallet = agentWalletDAO.getWallet(agentUserId, roleType);

      if (wallet != null) {
        wallet.setRoleType(roleType);
        // 查询代理商代理地区分润金额
        AgentWalletVO agentIncome = agentWalletDAO.getAgentIncome(agentUserId);
        if (agentIncome != null && agentIncome.getAgentIncome() != null) {
          wallet.setAgentIncome(agentIncome.getAgentIncome());
        }

        // 查询代理商平台任务收益（包含月任务、日任务、总任务）
        AgentWalletVO taskIncome = agentWalletDAO.getAgentTaskIncome(agentUserId);
        if (taskIncome != null) {
          wallet.setMonthTaskIncome(taskIncome.getMonthTaskIncome());
          wallet.setDayTaskIncome(taskIncome.getDayTaskIncome());
          wallet.setTotalTaskIncome(taskIncome.getTotalTaskIncome());
        }

        // 查询代理商邀请收益
        AgentWalletVO refereeIncome = agentWalletDAO.getRefereeAgentIncome(agentUserId);
        if (refereeIncome != null && refereeIncome.getRefereeAgentIncome() != null) {
          wallet.setRefereeAgentIncome(refereeIncome.getRefereeAgentIncome());
        }

        // 计算总余额
        if (wallet.getBalance() != null && wallet.getFrozenBalance() != null) {
          wallet.setTotalBalance(wallet.getBalance().add(wallet.getFrozenBalance()));
        }
      }

      return wallet;
    }
    if (roleType.equals(IntegerEnum.ROLE_TYPE_SALESMAN.getCode())) {
      AgentWalletVO wallet = agentWalletDAO.getWallet(agentUserId, roleType);
      if (wallet != null) {
        wallet.setRoleType(roleType);
      }
      return wallet;
    }
    return null;
  }
}
